import { 
	login, 
	getUserList, 
	getUserName, 
	getUser, 
	getDepartmentList, 
	updateUser, 
	getActivityList,
	regularization,
	leave,
	addInsurance,
	updateInsurance,
	addUser,
	getPositionList,
	updatePassword,
	getUserSelectList,
	getReddotApi
} from '@/api'
// 获取token|设置token|删除token的函数
import { getToken, setToken, removeToken, setUserId , setUserDepartmentId, setUserNickName, setUserAuthority, setRedDot, getRedDot} from '@/unit/auth'
import { toDay, exdateDiff } from '@/unit/myDate'

const getDefaultState = () => {
	return {
		token: getToken(),
		userInfo: [],
		departmentInfo: [],
		userIds: [],
		userInfoAll: [],
		positionInfo: [],
		positionValue: 0,
		userNameValue: 0,
		userInfoData: [
			{}, {
				user_photo: '',
				sex: 0,
				state: 0,
			}, [], {}
		],
		userInfoDataUpdate: {},
		DepartmentList: [],
		userStateValue: true,
		reddot: {}
	}
}
const state = getDefaultState()

// 封装的方法
// userInfo与复选框id绑定的方法
const myUserInfo = () =>{
	let arr = []
	let index = 0
	for(let i = 0 ; i < state.userIds.length ; i++){
		for(let j = 0 ; j < state.userInfoAll.length ; j++){
			if (state.userIds[i] === state.userInfoAll[j].id){
				arr[index] = state.userInfoAll[j]
				index++
			}
		}
	}
	state.userInfo = arr
}

const mutations = {
	// 存储token
	USERLOGIN(state, token){
		state.token = token
	},
	// 存储用户信息
	userInfo(state, res){
		state.userInfoAll = res.data
		state.userIds = res.data.map((item)=>{
			return item.id
		})
		state.departmentInfo = res.departmentArr
		state.positionInfo =res.positionArr
		myUserInfo()
	},
	// 修改复选框选中的用户id， 并修改userInfo
	updateIds(state, data){
		state.userIds = data
		myUserInfo()
	},
	// 名称查询
	userNameSelect(state, res){
		state.userIds = res.data.map((item)=>{
			return item.id
		})
		myUserInfo()
	},
	// 岗位筛选
	positionSelect(state, form){
		state.userIds = state.userInfoAll.map(item => {
			if(item.position_id === form.positionValue){
				return item.id
			}
		})
		myUserInfo()
	},
	// 保存用户详细信息
	getUser(state, res){
		state.userInfoData = res.data
		state.userInfoDataUpdate = res.data[1]
	},
	// 保存部门信息
	getDepartmentList(state, arr){
		state.DepartmentList = arr
	},
	// 改变是否显示离职员工
	changeUserStateValue(state){
		state.userStateValue = !state.userStateValue
	},
	// 更新红点信息
	reddot(state, data){
		state.reddot = data[0]
	}
}

const actions = {
	// 获得徽标信息
	async getReddot({commit}, query){
		const res = await getReddotApi(query)
		commit('reddot', res.data)
		// setRedDot(JSON.stringify(res.data.reddot[0]))
		return res
	},
	// 改变是否显示离职员工
	changeUserStateValue({commit}){
		commit('changeUserStateValue')
	},
	// 登录业务
	async startLogin({ commit }, userInfo) {
		let res = await login(userInfo)
		if (res.status === 0){
			commit('USERLOGIN', res.token)
			// 本地化持久存储
			setToken(res.token)
			setUserId(res.data.user_id)
			setUserDepartmentId(res.data.department_id)
			setUserNickName(res.data.nick_name)
			setUserAuthority(res.data.authority_info)
		}
		return res
	},
	// 获取用户列表信息
	async getUserList({ commit }) {
		let res = await getUserList()
		// 隐藏手机号中间4位
		const reg = /^(\d{3})\d{4}(\d{4})$/;
		for(let i = 0 ; i < res.data.length ; i++){
			res.data[i].phone = String(res.data[i].phone)
			res.data[i].phone = res.data[i].phone.replace(reg, '$1****$2')
			if (res.data[i].state == 2){
				res.data[i].post_time = exdateDiff(res.data[i].entry_date, res.data[i].leave_date)
			} else {
				res.data[i].post_time = exdateDiff(res.data[i].entry_date, toDay())
			}
		}
		commit('userInfo', res)
		return res
	},
	// 修改复选框选中的id值
	updateIds({ commit },data){
		commit('updateIds', data)
	},
	// 名称查询
	async userNameSelect({ commit }, data){
		let res = await getUserName(data)
		if(res.status === 0){
			commit('userNameSelect', res)
		}
		return res
	},
	// 岗位筛选
	positionSelect({commit}, form){
		commit('positionSelect', form)
	},
	// 请求用户详细信息
	async getUser({commit}, id){
		let res = await getUser(id)
		if (res.data[1].state === 2){
			res.data[1].post_time = exdateDiff(res.data[1].entry_date, res.data[1].leave_date)
		} else {
			res.data[1].post_time = exdateDiff(res.data[1].entry_date, toDay())
		}
		commit('getUser', res)
		return res
	},
	// 请求查询部门信息
	async getDepartmentList({commit}){
		let res = await getDepartmentList()
		let arr = []
		for(let i = 0; i < res.data.length; i++){
			arr[i] = {}
			arr[i].label = res.data[i].department1
			arr[i].value = res.data[i].department1_id
			if(res.data[i].departmentSon.length > 0){
				arr[i].children = []
				for(let j = 0; j < res.data[i].departmentSon.length; j++){
					arr[i].children[j] = {}
					arr[i].children[j].label = res.data[i].departmentSon[j].department2
					arr[i].children[j].value = res.data[i].department1_id + '.' + res.data[i].departmentSon[j].department2_id
				}
			}
		}
		commit('getDepartmentList', arr)
		let obj = {
			res,
			arr
		}
		return obj 
	},
	// 编辑用户信息
	async updateUser({commit}, data){
		let res = await updateUser(data)
		return res
	},
	// 请求获取权限列表
	async getActivityList(){
		let res = await getActivityList()
		return res
	},
	// 转正申请
	async regularization({commit}, data) {
		let res = await regularization(data)
		return res
	},
	// 离职申请
	async leave({commit}, data){
		let res = await leave(data)
		return res
	},
	// 添加保险
	async addInsurance({commit}, data){
		let res = await addInsurance(data)
		return res
	},
	// 修改保险
	async updateInsurance({commit}, data){
		let res = await updateInsurance(data)
		return res
	},
	// 新增用户
	async addUser({commit}, data){
		let res = await addUser(data)
		return res
	},
	// 查询岗位信息
	async getPositionList({commit}){
		let res = await getPositionList()
		return res
	},
	// 修改密码
	async updatePassword({commit}, data){
		let res = await updatePassword(data)
		return res
	},
	// 获取选择框用户下拉列表
	async getUserSelectList({commit}, data){
		let res = await getUserSelectList(data)
		return res
	},
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}